/**
 * 适配器，适配列数据，把旧属性适配到新属性上
 * @param cols  列
 * @param tableSetting   业务配置的设置项
 * @returns {*}
 */
import XEUtils from 'xe-utils';

let validRules = {}
const loadGridCol = (aryCols, tableSetting, obj) => {
  aryCols.forEach((colItem, i) => {
    if (colItem.children && colItem.children.length > 0) {
      loadGridCol(colItem.children, tableSetting, obj)
    } else {
      let colConfig = XEUtils.isString(colItem.config) ? XEUtils.toStringJSON(colItem.config) : colItem.config || {}
      if (!tableSetting.notValidate && colItem.requirement) { // 表格config中 notValidate 为true时不校验
        validRules[colItem.code] = [{
          require: true,
          message: colItem.name + '是必填项！'
        }]
      }
      if (!tableSetting.notValidate && colItem.requirement && colItem.datalength) { // 表格config中 notValidate 为true时不校验
        validRules[colItem.code] = [{
          min: 0,
          max: colItem.datalength,
          message: `名称长度在 0 到${colItem.datalength}个字符`
        }]
      }
      colItem["showOverflow"] = colItem.showOverflow || !!colItem.showOverflow;
      colItem["treeNode"] = colItem.treeNode || false;
      colItem["field"] = colItem.code || null;
      colItem["headerAlign"] = colConfig["headerAlign"] || "center"; // 表头文字位置
      colItem["align"] = colConfig["align"] || colItem.colAlign || (colItem.type == "number" || colItem.type == "amount") ? "right" : "center"; // 列位置
      colItem["fixed"] = colConfig["fixed"] || null
      colItem["sortable"] = colConfig["sortable"] || false; // 排序
      colItem["width"] = colItem.colWidth || null;
      colItem["minWidth"] = colItem.minWidth || '100px';
      colItem["visible"] = !!colItem.isVisiable;
      if (colItem.custom) {
        colItem.slots = {
          default: 'customCol'
        }
      }
      if (colItem.type == "select" || colItem.type == "selectTree") {
        colItem["formatter"] = ["formatSelect", colConfig];
      } else if (colItem.type == "amount") {
        colItem["formatter"] = ["formatAmount"];
      } else if (colItem.type == "gender") {
        colItem["formatter"] = ["formatSex"];
      } else if (colItem.type == "date") {
        colItem["formatter"] = ["formatDate"];
      }

      //可编辑的单元格
      if (colItem.isEdit) {
        colItem.type = colItem.type || "input";// 
        if (colItem.type == "input" || colItem.type == "showInput") {
          let editName = colItem.type === 'showInput' ? 'myShowInput' : 'myInput';
          let renderName = colItem.type === 'showInput' ? 'cellRender' : 'editRender';
          colItem[renderName] = {
            name: editName,
            props: {
              colConfig: colConfig,
              which: obj.which
            },
            events: {
              // change回调方法
              change: (row, column, key, val) => {
                obj.$emit("changeFunCallBack", row, column, key, val)
              },
              blur(row, column, key, val) {
                obj.$emit("blurFunCallBack", row, column, key, val)
              },
              focus(row, column, key, val) {
                obj.$emit("focusFunCallBack", row, column, key, val)
              },
              input(row, column, key, val) {
                obj.$emit("inputFunCallBack", row, column, key, val)
              },
              inputKey(row, column, key, val) {
                obj.$emit("inputKeyFunCallBack", row, column, key, val)
              },
            }
          }
        } else if (colItem.type == "number" || colItem.type == "amount") {
          colItem["editRender"] = {
            name: "myInputNumber",
            props: {
              colConfig: colConfig,
              which: obj.which,
            },
            events: {
              // change回调方法
              change: (row, column, key, val, currentObj) => {
                obj.$emit("changeFunCallBack", row, column, key, val, currentObj)
              },
              blur(row, column, key, val) {
                obj.$emit("blurFunCallBack", row, column, key, val)
              },
              focus(row, column, key, val) {
                obj.$emit("focusFunCallBack", row, column, key, val)
              },
            }
          };
        } else if (colItem.type == "selectTree" || colItem.type == "select" || colItem.type == "checkboxs" || colItem.type == "radios" || colItem.type == "autoComplete") {
          // colItem.align = colConfig["align"] || colItem.colAlign || 'left';
          obj.loadCacheData(colItem.code, colConfig.options || []);
          if (colItem.type == "selectTree") {
            colItem["editRender"] = {
              name: "mySelectTree",
              props: {
                colConfig: colConfig,
                which: obj.which,
              },
              events: {
                // change回调方法
                change: (row, column, key, val, currentObj) => {
                  obj.$emit("changeFunCallBack", row, column, key, val, currentObj)
                }
              }
            };
          } else if (colItem.type == "select") {
            colItem["editRender"] = {
              name: "mySelect",
              props: {
                colConfig: colConfig,
                which: obj.which,
              },
              events: {
                // change回调方法
                change: (row, column, key, val, currentObj) => {
                  obj.$emit("changeFunCallBack", row, column, key, val, currentObj)
                }
              }
            };
          } else if (colItem.type == "checkboxs") {
            colItem["cellRender"] = {
              name: "myCheckbox",
              props: {
                colConfig: colConfig,
                which: obj.which,
              },
              events: {
                // change回调方法
                change: (row, column, key, val, currentObj) => {
                  obj.$emit("changeFunCallBack", row, column, key, val, currentObj)
                }
              }
            };
          } else if (colItem.type == "radios") {
            colItem["cellRender"] = {
              name: "myRadio",
              props: {
                colConfig: colConfig,
                which: obj.which,
              },
              events: {
                // change回调方法
                change: (row, column, key, val) => {
                  obj.$emit("changeFunCallBack", row, column, key, val)
                }
              }
            };
          } else if (colItem.type == "autoComplete") {
            colItem["editRender"] = {
              name: "myAutoComplete",
              props: {
                colConfig: colConfig,
                which: obj.which,
              },
              events: {
                // change回调方法
                change: (row, column, key, val) => {
                  obj.$emit("changeFunCallBack", row, column, key, val)
                }
              }
            };
          }
        } else if (colItem.type === 'switch') {
          colItem["cellRender"] = {
            name: "mySwitch",
            props: {
              colConfig: colConfig,
              which: obj.which
            },
            events: {
              // change回调方法
              change: (row, column, key, val) => {
                obj.$emit("changeFunCallBack", row, column, key, val)
              }
            }
          };
        } else if (colItem.type === 'datePicker') {
          colItem["editRender"] = {
            name: "myDatePicker",
            props: {
              colConfig: colConfig,
              which: obj.which
            },
            events: {
              // change回调方法
              change: (row, column, key, val) => {
                obj.$emit("changeFunCallBack", row, column, key, val)
              }
            }
          };
        } else if (colItem.type === 'quarterDatepicker') {
          colItem["editRender"] = {
            name: "myQuarterDatepicker",
            props: {
              colConfig: colConfig,
              which: obj.which
            },
            events: {
              // change回调方法
              change: (row, column, key, val) => {
                obj.$emit("changeFunCallBack", row, column, key, val)
              }
            }
          };
        }
      }
      if (colItem.type === 'icon') {
        colItem["cellRender"] = {
          name: "myIcon",
          props: {
            colConfig: colConfig,
            which: obj.which
          },
        };
      } else if (colItem.type === "img") {
        colItem["cellRender"] = {
          name: "myImg",
          props: {
            colConfig: colConfig,
            which: obj.which
          },
        };
      } else if (colItem.type == "tooltipText") {
        colItem["cellRender"] = {
          name: "myTooltipText",
          props: {
            colConfig: colConfig,
            which: obj.which
          }
        };
      }
      else if (colItem.type == "jsonList") {
        colItem["cellRender"] = {
          name: "myJsonList", 
          props: {
            colConfig: colConfig,
            which: obj.which
          }
        };
      }
    }
  })
}

export const adapterColumns = (obj) => {
  let tableSetting = obj.tableConfigObj && obj.tableConfigObj.tableSetting ? obj.tableConfigObj.tableSetting : {};
  let aryCols = obj.gridColumnData;
  // let cloneCols= XEUtils.clone(aryCols, true);
  loadGridCol(aryCols, tableSetting, obj)
  if (tableSetting.operate) {
    aryCols.push({
      field: '',
      title: '操作',
      width: tableSetting.operateWidth,
      align: tableSetting.colAlign,
      headerAlign: 'center',
      fixed: 'right',
      slots: {
        default: 'operateCol'
      }
    });
  }
  return { aryCols, validRules };
}
